﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ppt = Microsoft.Office.Interop.PowerPoint;

namespace ObjectModel.PowerPoint
{
    /// <summary>
    /// A generic class that updates the state of a dictionary whose keys are PowerPoint
    /// slide IDs in response to changes to the slide structure of a Presentation object.
    /// </summary>
    public class SlideKeyDictionaryController<T>
    {
        readonly Dictionary<int, T> mLinkedObjectCollections;
        readonly PresentationEvents mEvents;
        
        public SlideKeyDictionaryController(Dictionary<int, T> dict, Ppt.Presentation pres)
        {
            mLinkedObjectCollections = dict;
            mEvents = Globals.PresentationEvents[pres];
            //mEvents.ShapeSelectionChanged += OnSelectionChanged;
            mEvents.SlideDeleted += OnSlideDeleted;
            //mEvents.ShapeDeleted += OnShapeDeleted;
        }

        //void OnSelectionChanged(Ppt.Selection Sel)
        //{
        //    int slideID = Sel.SlideRange[1].SlideID;

        //    if (mLinkedObjectCollections.ContainsKey(slideID))
        //    {
        //        LinkedObjectCollection objects = mLinkedObjectCollections[slideID];

        //        // Set the selected item only if a single shape is selected.
        //        if (Sel.ShapeRange.Count == 1)
        //            objects.SelectedItem = objects.FirstOrDefault(o => o.IsSelected);
        //        else
        //            objects.SelectedItem = null;
        //    }
        //}

        void OnSlideDeleted(object sender, SlideChangedEventArgs e)
        {
            if (mLinkedObjectCollections.ContainsKey(e.SlideInfo.Id))
                mLinkedObjectCollections.Remove(e.SlideInfo.Id);
        }

        //void OnShapeDeleted(object sender, ShapeChangedEventArgs e)
        //{
        //    if (mLinkedObjectCollections.ContainsKey(e.ShapeInfo.SlideId))
        //    {
        //        LinkedObjectCollection objects = mLinkedObjectCollections[e.ShapeInfo.SlideId];

        //        foreach (LinkedObject o in objects)
        //            if (o.TargetShape.ShapeId == e.ShapeInfo.Id)
        //                objects.Remove(o);
        //    }
        //}
    }
}
